package com.example.select2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner select2 = findViewById(R.id.select2);

        String[] icons = new String[]{"icon1","icon2","icon3"};
        String[] values = new String[]{"金星","火星","木星"};

        List<Map<String,String>> data = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Map<String,String> m = new HashMap<>();
            m.put("icon",icons[i]);
            m.put("value",values[i]);
            data.add(m);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(
                this,
                data,
                R.layout.select_item,
                new String[]{"icon","value"},
                new int[]{R.id.icon,R.id.value}
        );

        select2.setAdapter(simpleAdapter);


    }
}